/*
 ============================================================================
 Name		: CelestialSphereAppView.cpp
 Author	  : Den Grigorenko
 Copyright   : Copyright (c) 2008 Den123
 Description : Application view implementation
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>
#include <AknDef.h>   // KEikDynamicLayoutVariantSwitch
#include <stringloader.h>

#include "CelestialSphereAppView.h"
#include "FontData.h"
#include "SputnikBarDrawer.h"
#include "SputnikDrawer.h"
#include "Magnetic.h"

#include <CelestialSphere_0xEEC98BE4.rsg>

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CCelestialSphereAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CCelestialSphereAppView* CCelestialSphereAppView::NewL(const TRect& aRect)
{
	CCelestialSphereAppView* self = CCelestialSphereAppView::NewLC(aRect);
	CleanupStack::Pop(self);
	return self;
}

// -----------------------------------------------------------------------------
// CCelestialSphereAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CCelestialSphereAppView* CCelestialSphereAppView::NewLC(const TRect& aRect)
{
	CCelestialSphereAppView* self = new ( ELeave ) CCelestialSphereAppView;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
}

// -----------------------------------------------------------------------------
// CCelestialSphereAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CCelestialSphereAppView::ConstructL(const TRect& /*aRect*/ )
{
	// Create a window for this application view
	CreateWindowL();

	iFontData = new (ELeave) SFontData();
	iFontData->Setup();
	
	iWaitStr = StringLoader::LoadL( R_WAIT_STR );
        
	// array for custom controls
	InitComponentArrayL(); 
	
	// new custom control
	iSputnikDrawer = new (ELeave) CSputnikDrawer();	
	Components().AppendLC( iSputnikDrawer );
	iSputnikDrawer->ConstructL( this, iFontData, &iSatData, *iWaitStr );
	CleanupStack::Pop( iSputnikDrawer ) ;  	
	
	// new custom control
	iSputnikBarDrawer = new (ELeave) CSputnikBarDrawer();
	Components().AppendLC( iSputnikBarDrawer );
	iSputnikBarDrawer->ConstructL( this, iFontData, &iSatData );
	CleanupStack::Pop( iSputnikBarDrawer ) ;  
	

    iFinder = CSensorFinder :: NewL( *this );
	
	// Set the windows size
	// SetExtentToWholeScreen();
    
    

	// Activate the window, which makes it ready to be drawn
	ActivateL();
}

// -----------------------------------------------------------------------------
// CCelestialSphereAppView::CCelestialSphereAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CCelestialSphereAppView::CCelestialSphereAppView()
{
	// No implementation required
}

// -----------------------------------------------------------------------------
// CCelestialSphereAppView::~CCelestialSphereAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CCelestialSphereAppView::~CCelestialSphereAppView()
{
	if( iFontData )
	{
		delete iFontData;
		iFontData = NULL;
	}
	
	if( iWaitStr )
	{
		delete iWaitStr;
		iWaitStr = NULL;
	}

    delete iFinder, iFinder = NULL;
}


void CCelestialSphereAppView::Update( TPositionSatelliteInfo& aData )
{
	iSatData = aData;
	if( iSputnikDrawer && iSputnikBarDrawer )
	   DrawNow();
}

void CCelestialSphereAppView::UpdateError( TInt /*aError*/ )
{
	iSatData.ClearSatellitesInView(); // reading completed with error -> empty screen	
	if( iSputnikDrawer && iSputnikBarDrawer )
	   DrawNow();
}


// -----------------------------------------------------------------------------
// CCelestialSphereAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CCelestialSphereAppView::Draw(const TRect& /*aRect*/) const
{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();

	// Gets the control's extent
	TRect drawRect(Rect());

	// Clears the screen
	gc.Clear(drawRect); 
}

// -----------------------------------------------------------------------------
// CCelestialSphereAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CCelestialSphereAppView::SizeChanged()
{
	// layout management
	if( iSputnikDrawer && iSputnikBarDrawer )
	{
   	   TRect rMain = Rect(),
	         rectSputnikDrawer = rMain,
	         rectSputnikBarDrawer = rMain;
   	   
   	   iFontData->Setup();
	
	   if( rMain.Height() >= rMain.Width() )
	   {	   // form top to bottom
		   rectSputnikDrawer.SetHeight( rMain.Width() /*static_cast<TInt>( rectSputnikDrawer.Height() * 0.75 ) */ ); // ���������� �������
		   iSputnikDrawer->SetRect( rectSputnikDrawer );
		   
		   rectSputnikBarDrawer.iTl.iY += rectSputnikDrawer.Height();
		   iSputnikBarDrawer->SetRect( rectSputnikBarDrawer );		   
	   }
	   else
	   {       // from left to right
		   rectSputnikDrawer.SetWidth( rMain.Height() /*static_cast<TInt>( rectSputnikDrawer.Width() * 0.75 ) */ ); // ���������� �������
		   iSputnikDrawer->SetRect( rectSputnikDrawer );
		   
		   rectSputnikBarDrawer.iTl.iX += rectSputnikDrawer.Width();
		   iSputnikBarDrawer->SetRect( rectSputnikBarDrawer );		   		   
	   }		   		   		 
	   DrawNow();
	}
}

void CCelestialSphereAppView::DataReceivedL(TInt aAngle, TInt aAccuracyLevel)
{
	if( aAccuracyLevel )
	    iSputnikDrawer->SetAngle( aAngle );
	DrawNow();
}



// End of File
